/*******************************************************************************
 * SDP Project AUT 2012
 * 
 *                ***********Driving Assistant************
 * Team:
 * Gisela, Deidre, Brijesh, Robert
 * 
 * This class sets up the main frame which houses the panels needed to make up
 * the UI components.
 * 
 * @version 1.0
 * 
 * Copyright (c) 2011 by DrivingAssistant Team, Inc., All Rights Reserved.
 * 
 ******************************************************************************/
package com.drivingAssistant.gui;

import com.drivingAssistant.model.ArduinoLightBar;
import com.drivingAssistant.model.SoundOff;
import com.drivingAssistant.model.UserGuide;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.LineUnavailableException;
import javax.swing.*;

/**
 *
 * Driving Assistant UI
 * 
 */
public class DrivingAssistantUI extends JFrame {
    
    // constants
    private static final String beep1Clip = "./sound/beep-01.wav";
    private static final String beep2Clip = "./sound/beep-02.wav";
    private static final String beep3Clip = "./sound/beep-03.wav";
    private static final String beep4Clip = "./sound/beep-04.wav";
    private static final String beep5Clip = "./sound/beep-05.wav";
    private static final String beep6Clip = "./sound/beep-06.wav";
    private static final String lazerClip = "./sound/lazer.wav";
    private static final String hornClip = "./sound/horn.wav";
    private static final String redAlertClip = "./sound/redalert.wav";
    private static final int WIDTH = 605;    // Pixels.
    private static final int HEIGHT = 615;
    private static final int WP_WIDTH = 320;    // Pixels.
    private static final int WP_HEIGHT = 435;
    
    
    // variables
    java.util.Timer timer; // testing for output data to jtextarea
    JPanel eastPanel, westPanel;
    JRadioButton beep1btn, beep2btn, beep3btn, beep4btn, beep5btn, beep6btn;
    JRadioButton lazerbtn, hornbtn, redAlertbtn;
    Component[] comps;
    SoundOff soundOff;
    ArduinoReader ardReader;
    Distance distancePanel;
    Light lightPanel;
    Logo logoPanel;
    Sound soundPanel;
    UserGuide userGuide;
    ArduinoLightBar task;
    File clipFile = null;
    Color colour;
    JMenuBar menuBar;
    JMenu menu;
    JMenuItem itemExit;
    JMenuItem itemGuide;
    private String soundClip = "";
    private String reading = "";
    private boolean on = true;
    
    /**
     * Constructor to set up objects for the DrivingAssistantUI class
     */
    public DrivingAssistantUI() {
        eastPanel = new JPanel();
        westPanel = new JPanel();
        soundOff = new SoundOff();
        ardReader = new ArduinoReader();
        distancePanel = new Distance();
        lightPanel = new Light();
        logoPanel = new Logo();
        soundPanel = new Sound();
        userGuide = new UserGuide();
        task = new ArduinoLightBar();
        colour = new Color(210, 105, 30, 255);
        // use this to access components, still in test mode
        comps = distancePanel.getComponents(); 
        // this timer is not completed at this time
        timer = new java.util.Timer();
        timer.schedule(new TimeChanger(), 0, 20);
        
        setup();
        
    }

    /**
     * Setup the components for the UI.
     */
    private void setup() {
        setPreferredSize(new Dimension(WIDTH, HEIGHT));
        setLayout(new BorderLayout(5,5));
        setTitle("Driving Assistant");
        getContentPane().setBackground(colour);
        
        menuBar = new JMenuBar();
        menu = new JMenu("Options");
        
        itemExit = new JMenuItem("Exit");
        itemExit.setMnemonic('E');
        itemExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,
                                                       InputEvent.CTRL_MASK));
        itemExit.addActionListener(new AbstractAction("Exit") {

            @Override
            public void actionPerformed(ActionEvent e) {
                Object[] options = {"Quit", "Continue"};
                int choice = JOptionPane.showOptionDialog(
                        rootPane,
                        "Please confirm option", 
                        "Options",
                        JOptionPane.YES_NO_CANCEL_OPTION,
                        JOptionPane.QUESTION_MESSAGE,
                        null,
                        options,
                        options[0]);
                if (choice == 0) {
                    System.exit(0);
                }
            }
        });
        
        itemGuide = new JMenuItem("User Guide");
        itemGuide.setMnemonic('I');
        itemGuide.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I,
                                                       InputEvent.CTRL_MASK));
        itemGuide.addActionListener(new AbstractAction("User Guide") {

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(
                        null,
                        userGuide.drivingAssistantUserGuide(),
                        "Driving Assistant User Guide",
                        JOptionPane.PLAIN_MESSAGE);
            }
        });
        
        
        menu.setMnemonic(KeyEvent.VK_O);
        menu.add(itemGuide);
        menu.add(itemExit);
        
        this.setJMenuBar(menuBar);
        menuBar.add(menu);
        
        beep1btn = new JRadioButton("Beep1");
        beep1btn.setOpaque(false);
        beep1btn.addActionListener(new AbstractAction("Beep1") {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (beep1btn.isSelected()) {
                    try {
                        soundOff.setClip(beep1Clip);
                        soundOff.playClip();
                    } catch (LineUnavailableException ex) {
                        Logger.getLogger(DrivingAssistantUI.class.getName())
                                .log(Level.SEVERE, null, ex);
                    }
                }
            }
        });
        beep2btn = new JRadioButton("Beep2");
        beep2btn.setOpaque(false);
        beep2btn.addActionListener(new AbstractAction("Beep2") {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (beep2btn.isSelected()) {
                    try {
                        soundOff.setClip(beep2Clip);
                        soundOff.playClip();
                    } catch (LineUnavailableException ex) {
                        Logger.getLogger(DrivingAssistantUI.class.getName())
                                .log(Level.SEVERE, null, ex);
                    }
                }
                // used for testing purposes
//                System.out.println("val1 = " + distancePanel.getDistance());
//                System.out.println("val2 = " + distancePanel.getDis());
//                if (distancePanel.getDistance() == distancePanel.getDis()) {
////                    System.out.println("here in if btnbeep2...");
//                    try {
//                        soundOff.setClip(beep2Clip);
//                        soundOff.playClip();
//                        getRead();
//                    } catch (LineUnavailableException ex) {
//                        Logger.getLogger(DrivingAssistantUI.class.getName())
//                                .log(Level.SEVERE, null, ex);
//                    }
//                }
            }
        });
        beep3btn = new JRadioButton("Beep3");
        beep3btn.setOpaque(false);
        beep3btn.addActionListener(new AbstractAction("Beep3") {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (beep3btn.isSelected()) {
                    try {
                        soundOff.setClip(beep3Clip);
                        soundOff.playClip();
                    } catch (LineUnavailableException ex) {
                        Logger.getLogger(DrivingAssistantUI.class.getName())
                                .log(Level.SEVERE, null, ex);
                    }
                }
            }
        });
        beep4btn = new JRadioButton("Beep4");
        beep4btn.setOpaque(false);
        beep4btn.addActionListener(new AbstractAction("Beep4") {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (beep4btn.isSelected()) {
                    try {
                        soundOff.setClip(beep4Clip);
                        soundOff.playClip();
                    } catch (LineUnavailableException ex) {
                        Logger.getLogger(DrivingAssistantUI.class.getName())
                                .log(Level.SEVERE, null, ex);
                    }
                }
            }
        });
        beep5btn = new JRadioButton("Beep5");
        beep5btn.setOpaque(false);
        beep5btn.addActionListener(new AbstractAction("Beep5") {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (beep5btn.isSelected()) {
                    try {
                        soundOff.setClip(beep5Clip);
                        soundOff.playClip();
                    } catch (LineUnavailableException ex) {
                        Logger.getLogger(DrivingAssistantUI.class.getName())
                                .log(Level.SEVERE, null, ex);
                    }
                }
            }
        });
        beep6btn = new JRadioButton("Beep6");
        beep6btn.setOpaque(false);
        beep6btn.addActionListener(new AbstractAction("Beep6") {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (beep6btn.isSelected()) {
                    try {
                        soundOff.setClip(beep6Clip);
                        soundOff.playClip();
                    } catch (LineUnavailableException ex) {
                        Logger.getLogger(DrivingAssistantUI.class.getName())
                                .log(Level.SEVERE, null, ex);
                    }
                }
            }
        });
        lazerbtn = new JRadioButton("Lazer");
        lazerbtn.setOpaque(false);
        lazerbtn.addActionListener(new AbstractAction("Lazer") {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (lazerbtn.isSelected()) {
                    try {
                        soundOff.setClip(lazerClip);
                        soundOff.playClip();
                    } catch (LineUnavailableException ex) {
                        Logger.getLogger(DrivingAssistantUI.class.getName())
                                .log(Level.SEVERE, null, ex);
                    }
                }
            }
        });
        hornbtn = new JRadioButton("Horn");
        hornbtn.setOpaque(false);
        hornbtn.addActionListener(new AbstractAction("Horn") {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (hornbtn.isSelected()) {
                    try {
                        soundOff.setClip(hornClip);
                        soundOff.playClip();
                    } catch (LineUnavailableException ex) {
                        Logger.getLogger(DrivingAssistantUI.class.getName())
                                .log(Level.SEVERE, null, ex);
                    }
                }
            }
        });
        redAlertbtn = new JRadioButton("RedAlert");
        redAlertbtn.setOpaque(false);
        redAlertbtn.addActionListener(new AbstractAction("RedAlert") {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (redAlertbtn.isSelected()) {
                    try {
                        soundOff.setClip(redAlertClip);
                        soundOff.playClip();
                    } catch (LineUnavailableException ex) {
                        Logger.getLogger(DrivingAssistantUI.class.getName())
                                .log(Level.SEVERE, null, ex);
                    }
                }
            }
        });
        ButtonGroup btnGroup = new ButtonGroup();
        btnGroup.add(beep1btn);
        btnGroup.add(beep2btn);
        btnGroup.add(beep3btn);
        btnGroup.add(beep4btn);
        btnGroup.add(beep5btn);
        btnGroup.add(beep6btn);
        btnGroup.add(lazerbtn);
        btnGroup.add(hornbtn);
        btnGroup.add(redAlertbtn);
        
        JPanel grpPanel = new JPanel();
        JPanel northRadioPanel = new JPanel();
        JPanel eastRadioPanel = new JPanel();
        JPanel westRadioPanel = new JPanel();
        northRadioPanel.setPreferredSize(new Dimension(30, 50));
        eastRadioPanel.setPreferredSize(new Dimension(30, 20));
        westRadioPanel.setPreferredSize(new Dimension(30, 20));
        northRadioPanel.setOpaque(false);
        eastRadioPanel.setOpaque(false);
        westRadioPanel.setOpaque(false);
        grpPanel.setOpaque(false);
        
        grpPanel.setLayout(new GridLayout(3, 3, 5, 5));
        grpPanel.add(beep1btn);
        grpPanel.add(beep2btn);
        grpPanel.add(beep3btn);
        grpPanel.add(beep4btn);
        grpPanel.add(beep5btn);
        grpPanel.add(beep6btn);
        grpPanel.add(lazerbtn);
        grpPanel.add(hornbtn);
        grpPanel.add(redAlertbtn);
        
        soundPanel.setLayout(new BorderLayout());
        soundPanel.add(grpPanel, BorderLayout.CENTER);
        soundPanel.add(northRadioPanel, BorderLayout.NORTH);
        soundPanel.add(eastRadioPanel, BorderLayout.EAST);
        soundPanel.add(westRadioPanel, BorderLayout.WEST);
        
        westPanel.setLayout(new BorderLayout(5,5));
        westPanel.setBackground(colour);
        eastPanel.setLayout(new BorderLayout(5,5));
        westPanel.add(distancePanel, BorderLayout.NORTH);
        westPanel.add(soundPanel, BorderLayout.CENTER);
        westPanel.setPreferredSize(new Dimension(WP_WIDTH, WP_HEIGHT));
        eastPanel.add(lightPanel, BorderLayout.CENTER);
        
        this.add(logoPanel, BorderLayout.NORTH);
        this.add(westPanel, BorderLayout.WEST);
        this.add(eastPanel, BorderLayout.EAST);
    }

    private void getRead() {
        System.out.println("text from Text Area = " + Distance.ardTextArea.getText());
    }
    
    /**
     * This class is still in test stages
     */
    private static class TimeChanger extends TimerTask {

        public TimeChanger() {
        }

        @Override
        public void run() {
//            if(ardReader.getClass().g)
        }
    }
    
    // used for testing purposes
    public static void main(String[] args) {
        final DrivingAssistantUI da = new DrivingAssistantUI();
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                da.pack();
                da.setDefaultCloseOperation(DrivingAssistantUI.DISPOSE_ON_CLOSE);
                da.setLocationRelativeTo(null);
                da.setVisible(true);
            }
        });
    }
    
}
